package com.smartcruise.main.mainFragment.messageNotice;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.smartcruise.R;
import com.smartcruise.base.BaseNormalView;
import com.smartcruise.data.PushData;
import com.smartcruise.data.ReportData;
import com.smartcruise.data.cruiseData.CruiseData;
import com.smartcruise.report.EditType;
import com.smartcruise.report.ReportDataEditActivity;
import com.smartcruise.task.TaskWithMapActivity;
import com.smartcruise.util.Key;
import com.smartcruise.util.TimeUtils;
import com.smartcruise.util.customView.CircleImage;
import com.smartcruise.util.network.callBack.SimpleCallBack;
import com.smartcruise.util.network.netUtils.NetCollection;
import com.smartcruise.util.network.netUtils.Result;

import java.util.Calendar;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.realm.OrderedRealmCollection;
import io.realm.Realm;
import io.realm.RealmBaseAdapter;

/**
 * Created by FengChaoQun
 * on 2018/1/21 0021
 */

public class MessageNoticeAdapter extends RealmBaseAdapter<PushData> {

    private Context context;
    private Realm realm;
    private BaseNormalView mView;

    public MessageNoticeAdapter(@Nullable OrderedRealmCollection<PushData> data, BaseNormalView mView) {
        super(data);
        this.context = mView.getActivity();
        this.realm = mView.getRealm();
        this.mView = mView;
    }

    @NonNull
    @Override
    public View getView(final int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        final ViewHolder viewHolder;
        if (convertView == null) {
            convertView = View.inflate(context, R.layout.item_message_notice, null);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        final PushData pushData = getItem(position);
        viewHolder.time.setText(TimeUtils.getSimpleTimeString(pushData.getCreateTime()));
        switch (pushData.getType()) {
            case PushData.TYPE_NEW_TASK:
                viewHolder.typeCircle.setText("任务");
                viewHolder.title.setText("任务通知");
                viewHolder.subTitle.setText(pushData.getContent());
                break;
            case PushData.TYPE_TASK_AUDIT_RESULT:
                viewHolder.typeCircle.setText("审核");
                viewHolder.title.setText("审核通知");
                viewHolder.subTitle.setText(pushData.getContent());
                break;
//            case SIGN_IN:
//                viewHolder.typeCircle.setText("打卡");
//                viewHolder.title.setText("打卡通知");
//                viewHolder.subTitle.setText("任务名称");
//                viewHolder.reaDot.setVisibility(View.VISIBLE);
//                break;
            case PushData.TYPE_REPORT_AUDIT_RESULT:
                viewHolder.typeCircle.setText("事件");
                viewHolder.title.setText("事件通知");
                viewHolder.subTitle.setText(pushData.getContent());
                break;
//            case EXCEPTION:
//                viewHolder.typeCircle.setText("异常");
//                viewHolder.title.setText("异常通知");
//                viewHolder.subTitle.setText("任务名称");
//                viewHolder.reaDot.setVisibility(View.VISIBLE);
//                break;
            case PushData.TYPE_DEVICE_LOGIN:
                viewHolder.typeCircle.setText("上线");
                viewHolder.title.setText("上线通知");
                viewHolder.subTitle.setText(pushData.getContent());
                break;
            case PushData.TYPE_REVOKE_TASK:
                viewHolder.typeCircle.setText("撤销");
                viewHolder.title.setText("撤销巡护任务");
                viewHolder.subTitle.setText(pushData.getContent());
                break;
            default:
                viewHolder.typeCircle.setText("未知");
                viewHolder.title.setText("未知通知");
                viewHolder.subTitle.setText(pushData.getContent());
                break;
        }

        viewHolder.reaDot.setVisibility(pushData.isUnRead() ? View.VISIBLE : View.GONE);

        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (pushData.getType()) {
                    case PushData.TYPE_NEW_TASK:
                    case PushData.TYPE_TASK_AUDIT_RESULT:
                        gotoSeeTask(pushData);
                        break;
                    case PushData.TYPE_REPORT_AUDIT_RESULT:
                        gotoSeeReport(pushData);
                        break;
                }
                //点击过后设置为已读
                mView.getRealm().executeTransaction(new Realm.Transaction() {
                    @Override
                    public void execute(Realm realm) {
                        pushData.setUnRead(false);
                    }
                });
            }
        });

        return convertView;
    }

    /**
     * description:查看任务信息
     */

    private void gotoSeeTask(final PushData pushData) {
        final CruiseData first = realm.where(CruiseData.class)
                .equalTo(Key.id, pushData.getRelatedId())
                .findFirst();
        if (first != null) {
            TaskWithMapActivity.start(context, first.getId(), Calendar.getInstance());
        } else {
            if (PushData.TYPE_NEW_TASK.equals(pushData.getType())) {
                NetCollection.getOneNewTask(mView, pushData.getRelatedId(), new SimpleCallBack(mView) {
                    @Override
                    public void onSuccess(Result result) {
                        TaskWithMapActivity.start(context, pushData.getRelatedId(), Calendar.getInstance());
                    }
                });
            } else {
                NetCollection.getOneAuditedTask(pushData.getRelatedId(), mView, new SimpleCallBack(mView) {
                    @Override
                    public void onSuccess(Result result) {
                        TaskWithMapActivity.start(context, pushData.getRelatedId());
                    }
                });
            }
        }
    }

    /**
     * description:查看通报信息
     */

    private void gotoSeeReport(final PushData pushData) {
        ReportData data = realm.where(ReportData.class)
                .equalTo(Key.id, pushData.getRelatedId())
                .findFirst();
        if (data != null) {
            ReportDataEditActivity.start(context, data.isAuditSuccess() ? EditType.VIEW : EditType.MODIFY, data.getId());
        } else {
            NetCollection.getOneAuditedReport(pushData.getRelatedId(), mView, new SimpleCallBack(mView) {
                @Override
                public void onSuccess(Result result) {
                    ReportData data = realm.where(ReportData.class)
                            .equalTo(Key.id, pushData.getRelatedId())
                            .findFirst();
                    if (data != null) {
                        ReportDataEditActivity.start(context, data.isAuditSuccess() ? EditType.VIEW : EditType.MODIFY, data.getId());
                    }
                }
            });
        }
    }

    static class ViewHolder {
        @BindView(R.id.typeCircle)
        TextView typeCircle;
        @BindView(R.id.reaDot)
        CircleImage reaDot;
        @BindView(R.id.title)
        TextView title;
        @BindView(R.id.subTitle)
        TextView subTitle;
        @BindView(R.id.time)
        TextView time;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
